package com.swas.service.impl;

import com.swas.dao.UserDao;
import com.swas.entity.User;
import com.swas.entity.UserExample;
import com.swas.service.UserService;
import com.swas.util.MD5Util;
import com.swas.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lxysky
 */
@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(User user) {
        String string = MD5Util.generateMD5(user.getPassword(), 10, user.getUsername());
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(user.getUsername()).andPasswordEqualTo(string);
        List<User> list = userDao.selectByExample(example);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public User register(User user) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(user.getUsername());
        List<User> list = userDao.selectByExample(example);
        if (list.size() == 0) {
            user.setCreatetime(new Date());
            user.setUpdatetime(new Date());
            int i = userDao.insertSelective(user);
            return i>0?user:null;
        }
        return null;
    }

    @Override
    public List<User> getUsersByRoleId(Integer roleId) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andRoleidEqualTo(roleId);
        return userDao.selectByExample(example);
    }

    @Override
    public boolean insertOne(User user) {
        user.setCreatetime(new Date());
        user.setUpdatetime(new Date());
        return userDao.insertSelective(user) > 0;
    }

    @Override
    public boolean deleteOne(Integer id) {
        return userDao.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteSome(List<Integer> ids) {
        UserExample example = new UserExample();
        example.createCriteria().andIdIn(ids);
        return userDao.deleteByExample(example) > 0;
    }

    @Override
    public boolean updateOne(User user) {
        user.setUpdatetime(new Date());
        return userDao.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public boolean updateOnePassword(Integer id) {
        User user = userDao.selectByPrimaryKey(id);
        user.setPassword(MD5Util.generateMD5("123456", 10, user.getUsername()));
        user.setUpdatetime(new Date());
        return userDao.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public User selectOne(Integer id) {
        return userDao.selectByPrimaryKey(id);
    }

    @Override
    public List<UserVO> selectAll() {
        return userDao.selectAll();
    }

    @Override
    public List<UserVO> selectLike(String like, Integer role) {
        return userDao.selectLike(like, role);
    }

}
